<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

abstract class MY_Controller extends CI_Controller
{
	/**
     * _admin
     * 保存当前登录用户的信息
     *
     * @var object
     * @access  public
     **/
	public $attachment_size;
	public $attachment_type;
	public $attachment_type_str;
	
	/**
     * 构造函数
     *
     * @access  public
     * @return  void
     */
	function __construct()
	{
		parent::__construct();
		$this->_check_login();
		$this->load->library('acl');
		
		//提取允许的附件类型
		$query = $this->db->select('attachment_type,attachment_maxupload')->get('site_settings');
		$row = $query->row(); 
		$this->attachment_size = $row->attachment_maxupload.'MB';
		$this->attachment_type = explode(",",$row->attachment_type);
		if($this->attachment_type == ''){
			$this->attachment_type_str = '*.*';
		}else{
			foreach($this->attachment_type as $value){
				$this->attachment_type_str .= '*.'.$value.'; ';
			}
		}
	}
	
	//JSON成功或错误方法
	function rejsonsuccess($b=false,$str="未知错误"){
		if(!$b){
			echo json_encode(array("success"=>false,"errorMsg"=>$str));
		}else{
			echo json_encode(array("success"=>true)); 
		}	
	}
	// ------------------------------------------------------------------------

    /**
     * 检查用户是否登录
     *
     * @access  protected
     * @return  void
     */
	protected function _check_login()
	{
		if ( ! $this->session->userdata('user_name'))
		{   
			redirect('login');
			exit();
		}
	}
	
		//附件列表的公开方法
	/* 四个参数分别为：模块名，ID字符串，文件名字符串
	*/
	function attlist($m,$ids,$names,$sizes,$id='',$ps_id=''){
		if($ids != '' && $names != ''){
			$att_ids = explode(',',reduce_multiples($ids,',',true));
			$att_names = explode('*',reduce_multiples($names,'*',true));  //文件名分隔符是*号
			
			$att_sizes = explode(',',reduce_multiples($sizes,',',true));
			//$att_class = dechex(rand(00000000,99999999));
			
			//附件列表面板ID为panel_attlist_加模块名加ID号
			$str = "<div id='panel_attlist_".$m.$id."' class='easyui-panel' title='共（".count($att_names)."个）附件' style='padding:5px;width:100%;'>";

			$min_att = (count($att_ids)>count($att_names))?count($att_names):count($att_ids);

			for($i = 0; $i<$min_att;$i++){
				$attsuffix = strtolower(end(explode('.',$att_names[$i])));	//文件名后缀转小写
				$v_size = (empty($att_sizes[$i]))?'':' （'.byte_format($att_sizes[$i],2).'）';//如果去到了文件大小才显示括号和转换后的值
				$i_size = (empty($att_sizes[$i]))?'0':$att_sizes[$i];//如果出现未定义的偏移量，则以0字节填充
				$str .= "<div class='eattl' style='margin-top:5px;'>";				
				$str .= "<a href='javascript:void(0)' onclick=\"att_down('".$m."','".$att_ids[$i]."','".$att_names[$i]."')\" class='easyui-tooltip att_btn' style='text-align:left;'  data-options=\"
                    hideEvent: 'none',
					position: 'right',
					showDelay:0,
					hideDelay:0,
                    content: function(){
                        return $('#att_toolbar_".$id."_".$i."');
                    },
                    onShow: function(){
                        var t = $(this);
                        t.tooltip('tip').focus().unbind().bind('blur',function(){
                            t.tooltip('hide');
                        });
                    }
                \"><div class='icon-filetype-".$attsuffix."' style='width:16px; height:16px; float:left; margin:3px 5px 0 0;'></div>".$att_names[$i].$v_size."</a>";
				$str .= "<div style='display:none'>
				<div id='att_toolbar_".$id."_".$i."'>
					<a onmousedown='return false;' class='easyui-linkbutton att_toolbar_linkbutton' data-options=\"iconCls:'icon-add',plain:true\" onclick=\"att_down('".$m."','".$att_ids[$i]."','".$att_names[$i]."')\">下载</a>
					<a onmousedown='return false;' class='easyui-linkbutton att_toolbar_linkbutton' data-options=\"iconCls:'icon-cut',plain:true\" onclick=\"att_save('".$m."','".$att_ids[$i]."','".$att_names[$i]."')\">转存</a>";
				if($attsuffix == 'doc' || $attsuffix == 'docx' || $attsuffix == 'xls' || $attsuffix == 'xlsx'){
					$str .= "<a onmousedown='return false;' class='easyui-linkbutton att_toolbar_linkbutton' data-options=\"iconCls:'icon-zoom',plain:true\" onclick=\"att_read('0','".$m."','".$att_ids[$i]."','".$att_names[$i]."')\">阅读</a>";
					if($m == 'workflow'){
						$str .= "<a onmousedown='return false;' class='easyui-linkbutton att_toolbar_linkbutton' data-options=\"iconCls:'icon-edit',plain:true\" onclick=\"att_read('1','".$m."','".$att_ids[$i]."','".$att_names[$i]."')\">编辑</a>";
					}
				}
					
				//如果是工作流，则显示删除按钮
				if($m == 'workflow'){	
					$str .= "<a onmousedown='return false;' class='easyui-linkbutton att_toolbar_linkbutton' data-options=\"iconCls:'icon-cancel',plain:true\" onclick=\"att_del('".$id."','".$ps_id."','".$m."','".$att_ids[$i]."','".$att_names[$i]."','".$i_size."')\">删除</a>";
				}
				$str .= "</div></div></div>";
			}
			$str .= "</div>";
			clearstatcache();//清除读取的文件缓存
			return $str;
		}
	}
}
	